package com.third.bank.controller;

import com.third.bank.entity.SMSCode;
import com.third.bank.service.SMSCodeService;
import com.third.bank.service.UserService;
import com.third.bank.util.BaseResponse;
import com.third.bank.util.ErrorMessage;
import com.third.bank.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码controller
 */
@RestController
@RequestMapping("/sms")
public class SMSCodeController {

    @Autowired
    private SMSCodeService smsCodeService;

    @Autowired
    private UserService userService;

    /**
     * 生成验证码
     * @param tele
     * @return
     */
    @GetMapping("/getTele")
    public BaseResponse getCode(String tele){

//        验证该手机号是否已经开户
        boolean flag = userService.judgeExistPhone(tele);
        if (flag){
            String code = smsCodeService.sendCodeToSMS(tele);
            System.out.println(code);
            return ResultUtils.success(code);
        }else {
            return ResultUtils.error(ErrorMessage.NO_AUTH_ERROR,"该用户尚未开户");
        }
    }

    /**
     * 检验验证码
     * @param smsCode
     * @return
     */
    @PostMapping("/check")
    public boolean checkCode(@RequestBody SMSCode smsCode){
        System.out.println(smsCode);
        return smsCodeService.checkCode(smsCode);
    }
}
